﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;

using Refix.Core;
using Refix.Core.Repository;
using Refix.Repository.Models;

using Configuration = Refix.Core.Configuration;

namespace Refix.Repository.Controllers
{
    [HandleError]
    public class ComponentController : Controller
    {
        private RepositoryModel _repositoryModel = new RepositoryModel();

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult GetRevision(string component)
        {
            return Content(_repositoryModel.GetRevision(component).ToString());
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Get(string component, string version)
        {
            byte[] buffer = _repositoryModel.GetZipFile(component, version);
            if (buffer == null)
            {
                throw new HttpException(404, "Not found");
            }
            return new FileContentResult(buffer, "application/zip");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Upload(string component, string version)
        {
            if (string.IsNullOrEmpty(Configuration.RemoteRepository))
            {
                throw new HttpException(404, "Not found");
            }
            var localRepository = new LocalRepository();
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            if (localRepository.UploadToRemoteRepository(component, version))
            {
                return Content("Upload successful.");
            }
            return Content(builder.ToString());
        }

        [AcceptVerbs(HttpVerbs.Put)]
        public ActionResult Synchronize(string component, string email)
        {
            var zip = new byte[Request.InputStream.Length];
            Request.InputStream.Read(zip, 0, (int)Request.InputStream.Length);
            if (ConfigurationManager.AppSettings["Refix.Upload.Mode"] != "Direct")
            {
                throw new HttpException((int)HttpStatusCode.Unauthorized, "This repository does not support synchronizing.");
            }
            _repositoryModel.ProcessZipData(zip, component, null, email);
            return new EmptyResult();
        }

        [AcceptVerbs(HttpVerbs.Put)]
        public ActionResult AcceptUpload(string component, string version, string email)
        {
            var zip = new byte[Request.InputStream.Length];
            Request.InputStream.Read(zip, 0, (int)Request.InputStream.Length);
            if (ConfigurationManager.AppSettings["Refix.Upload.Mode"] == "Deny")
            {
                throw new HttpException((int)HttpStatusCode.Unauthorized, "This repository does not support uploading.");
            }
            _repositoryModel.ProcessZipData(zip, component, version, email);
            return new EmptyResult();
        }
    }
}